package com.flute.framework.search.highlight;

import java.util.*;

import com.flute.tools.util.PinyinUtil;
import com.flute.tools.util.StringUtil;

/**
 * 标红策略接口拼音处理的实现类
 * 
 * @author Dave
 * 
 */
public class PinyinMarkStrategy implements IMarkStrategy {

	/**
	 * 将全拼、模糊拼音、首字母拼音标红策略组装返回ArrayList
	 * 
	 * @see com.flute.Flute.mark.IMarkStrategy#getMarked(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public MarkPosition[] getMarked(String keyword, String searchString) {
		List<MarkPosition> set = new ArrayList<MarkPosition>();

		// 全拼
		String kp = StringUtil.getStandardString(keyword);
		for (String pinyin : PinyinUtil.getPinyinStrings(searchString.trim())) {
			if (kp.equals(pinyin)) {
				MarkPosition mp = new MarkPosition();
				mp.setSource(searchString);
				mp.setStart(0);
				mp.setEnd(searchString.length());
				set.add(mp);
				break;
			}
		}

		// // // 模糊拼音
		// kp = PinyinUtil.getFuzzyPinyin(keyword);
		// if (kp.equals(PinyinUtil.getFuzzyPinyin(searchString
		// .replaceAll(" ", "")))) {
		// MarkPosition mp = new MarkPosition();
		// mp.setSource(searchString);
		// mp.setStart(0);
		// mp.setEnd(searchString.length());
		// set.add(mp);
		// }

		// 首字母拼音
		for (String pinyin : PinyinUtil.getPinyinHeaders(searchString.trim())) {
			if (kp.equals(pinyin)) {
				MarkPosition mp = new MarkPosition();
				mp.setSource(searchString);
				mp.setStart(0);
				mp.setEnd(searchString.length());
				set.add(mp);
				break;
			}
		}

		return set.toArray(new MarkPosition[] {});
	}

}
